import { createRouter, createWebHashHistory } from 'vue-router'

export const routes = [
  {
    path: "/login", name: "login",
    component: () => import("@/views/login/index.vue")
  },
  {
    path: "/",
    name: "home",
    meta: { requiresAuth: true, title: "系统首页", iconName: "Search" },
    redirect: "/home",
    component: () => import("@/layout/MyLayout.vue"),
    children: [
      { path: "/home", meta: { requiresAuth: true,  title: "系统首页",iconName: "Lock" }, component: () => import("@/views/home/index.vue") }
    ]
  },
  {
    path: "/np",
    name: "np", 
    redirect: "/np",
    component: () => import("@/layout/MyLayout.vue"),
    children: [
      { path: "/np",component: () => import("@/views/home/np.vue") }
    ]
  },
  {
    path: "/banner",
    name: "banner",
    meta: { requiresAuth: true, iconName: "Bottom", title: "轮播图管理" },
    redirect: '/banner/list',
    component: () => import("@/layout/MyLayout.vue"),
    children: [
      {
        path: "/banner/list",
        meta: { requiresAuth: true, iconName: "Edit", title: "轮播图列表" },
        redirect: "/banner/list/home",
        component: () => import("@/views/banner/index.vue"),
        children: [
          { path: "/banner/list/home", meta: { requiresAuth: true, iconName: "Tools", title: "首页轮播图列表" }, component: () => import("@/views/banner/components/BannerHome.vue") },
          { path: "/banner/list/kind", meta: { requiresAuth: true, iconName: "location", title: "分类轮播图列表" }, component: () => import("@/views/banner/components/BannerKind.vue") }
        ]
      },
      {
        path: '/banner/add',
        meta: { requiresAuth: true, iconName: "Edit", title: "轮播图添加" },
        component: () => import("@/views/banner/add.vue")
      }
    ]
  },
  {
    path: "/pro",
    name: "pro",
    redirect: '/pro/list',
    meta: { requiresAuth: true, iconName: "Top", title: "产品管理" },
    component: () => import("@/layout/MyLayout.vue"),
    children: [
      { path: "/pro/list", meta: { requiresAuth: true, iconName: "Top", title: "产品列表" }, component: () => import("@/views/pro/index.vue") },
      { path: "/pro/filter", meta: { requiresAuth: true, iconName: "Top", title: "筛选列表" }, component: () => import("@/views/pro/index.vue") }
    ]
  },
  {
    path: "/account",
    name: "account",
    redirect: '/account/admin',
    meta: { requiresAuth: true, iconName: "location", title: "账户管理" },
    component: () => import("@/layout/MyLayout.vue"),
    children: [
      { path: "/account/admin", meta: { requiresAuth: true, iconName: "location", title: "管理员列表" }, component: () => import("@/views/account/MyAdmin.vue") },
      { path: "/account/user", meta: { requiresAuth: true, iconName: "location", title: "用户列表" }, component: () => import("@/views/account/MyUser.vue") }
    ]
  },
  
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})
import { useUserStore } from '@/stores/useUser';
import { useRoute } from 'vue-router';
router.beforeEach((to, from) => {
  const useUser = useUserStore();
  // 而不是去检查每条路由记录
  // to.matched.some(record => record.meta.requiresAuth)
  if (to.meta.requiresAuth && !useUser.isLogin) {
    // 此路由需要授权，请检查是否已登录
    // 如果没有，则重定向到登录页面
    return {
      path: '/login',
      // 保存我们所在的位置，以便以后再来
      query: { redirect: to.fullPath },
    }
  }
})
export default router
